﻿using System;
using System.Windows.Media;
using System.Windows.Media.Media3D;

using SBPweb.Presentation.Windows.WPF.Utils;

namespace SBPweb.Presentation.Windows.WPF.Primitives
{

    public class CubeMesh: MeshBase
    {

        #region Constructor logic

        public CubeMesh()
            :base()
        {
            pTextureCoordinateGeometry = Textures.TextureCoordinateGeometries.Planar;
        }

        #endregion

        #region Protected members

        protected int pWidth = 1;

        #endregion

        #region Public property accessors

        public virtual int Width
        {
            get
            {
                return pWidth;
            }
            set
            {
                pWidth = value;
                CalculateGeometry();
            }
        }

        #endregion

        #region Overridden methods

        protected override void CalculateGeometry()
        {
            double widthHalf = pWidth / 2.0f;
            double minusWidthHalf = widthHalf * -1.0f;

            InitializeCollections();

            // vertexes
            pMesh.Positions.Add(new Point3D(minusWidthHalf, minusWidthHalf, minusWidthHalf));
            pMesh.Positions.Add(new Point3D(widthHalf, minusWidthHalf, minusWidthHalf));
            pMesh.Positions.Add(new Point3D(minusWidthHalf, minusWidthHalf, widthHalf));
            pMesh.Positions.Add(new Point3D(widthHalf, minusWidthHalf, widthHalf));
            pMesh.Positions.Add(new Point3D(minusWidthHalf, widthHalf, minusWidthHalf));
            pMesh.Positions.Add(new Point3D(widthHalf, widthHalf, minusWidthHalf));
            pMesh.Positions.Add(new Point3D(minusWidthHalf, widthHalf, widthHalf));
            pMesh.Positions.Add(new Point3D(widthHalf, widthHalf, widthHalf));

            // triangles
            pMesh.TriangleIndices.Add(0);
            pMesh.TriangleIndices.Add(3);
            pMesh.TriangleIndices.Add(2);
            
            pMesh.TriangleIndices.Add(3);
            pMesh.TriangleIndices.Add(0);
            pMesh.TriangleIndices.Add(1);

            pMesh.TriangleIndices.Add(4);
            pMesh.TriangleIndices.Add(7);
            pMesh.TriangleIndices.Add(5);

            pMesh.TriangleIndices.Add(7);
            pMesh.TriangleIndices.Add(4);
            pMesh.TriangleIndices.Add(6);

            pMesh.TriangleIndices.Add(0);
            pMesh.TriangleIndices.Add(5);
            pMesh.TriangleIndices.Add(1);

            pMesh.TriangleIndices.Add(4);
            pMesh.TriangleIndices.Add(5);
            pMesh.TriangleIndices.Add(0);

            pMesh.TriangleIndices.Add(2);
            pMesh.TriangleIndices.Add(4);
            pMesh.TriangleIndices.Add(0);

            pMesh.TriangleIndices.Add(6);
            pMesh.TriangleIndices.Add(4);
            pMesh.TriangleIndices.Add(2);

            pMesh.TriangleIndices.Add(7);
            pMesh.TriangleIndices.Add(6);
            pMesh.TriangleIndices.Add(3);

            pMesh.TriangleIndices.Add(2);
            pMesh.TriangleIndices.Add(3);
            pMesh.TriangleIndices.Add(6);

            pMesh.TriangleIndices.Add(1);
            pMesh.TriangleIndices.Add(7);
            pMesh.TriangleIndices.Add(3);

            pMesh.TriangleIndices.Add(7);
            pMesh.TriangleIndices.Add(1);
            pMesh.TriangleIndices.Add(5);

            // textures
            ApplyTextureCoordinates();
        }

        #endregion

    }
}
